import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_study_app/util/dialog_util.dart';
import 'package:flutter_study_app/widget/my_app_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_study_app/widget/stamp_image.dart';
import 'package:image_picker/image_picker.dart';

class ImageWatermarkPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _ImageWatermarkPageState();
}

class _ImageWatermarkPageState extends State<ImageWatermarkPage> {
  List<String> _ways = ['相机', '相册'];
  final picker = ImagePicker();

  String? _selectedPhoto;

  _selectPhotos() {
    DialogUtil.bottomListSelectDialog(context, 2,
        title: '选取图片',
        buildItemText: (index) => _ways[index],
        hasSelectedItem: (index) => false,
        onSelectItem: (index) {
          _getPickImage(index == 0 ? ImageSource.camera : ImageSource.gallery);
        });
  }

  _getPickImage(ImageSource imgSource) async {
    XFile? imageFile = await picker.pickImage(source: imgSource);
    if (imageFile != null) {
      _selectedPhoto = imageFile.path;
      //setState(() {});
    }
  }

  _addWatermark() {
    if (_selectedPhoto != null) {
      EasyLoading.show(status: "请稍等...");
      StampImage.create(
        context: context,
        image: File(_selectedPhoto!),
        children: [
          Positioned(
            left: 10.w,
            bottom: 10.w,
            child: Container(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '曾龙（20081547260）2021-11-30 16:11:01',
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 14.sp,
                      fontWeight: FontWeight.w500),
                ),
                Container(
                  margin: EdgeInsets.only(top: 5.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Icon(
                        Icons.my_location,
                        size: 20.w,
                        color: Colors.white,
                      ),
                      SizedBox(
                        width: 4.w,
                      ),
                      Text(
                        '27.7224568,112.011647',
                        style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 5.w),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Icon(
                        Icons.location_on_outlined,
                        size: 20.w,
                        color: Colors.white,
                      ),
                      SizedBox(
                        width: 4.w,
                      ),
                      Text(
                        '湖南省长沙市岳麓区',
                        style: TextStyle(
                            fontSize: 14.sp,
                            color: Colors.white,
                            fontWeight: FontWeight.w500),
                      ),
                    ],
                  ),
                ),
              ],
            )),
          )
        ],
        onSuccess: (file) {
          EasyLoading.dismiss();
          print("file_path:${file.path}");
          setState(() {
            _selectedPhoto = file.path;
          });
        },
      );
    } else
      EasyLoading.showToast("请选择图片");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(title: '图片加水印'),
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: Container(
                alignment: Alignment.center,
                child: Container(
                  width: 150.w,
                  child: _selectedPhoto != null
                      ? Image.file(
                          File(_selectedPhoto!),
                          fit: BoxFit.fitWidth,
                        )
                      : Text(
                          '请选择图片',
                          textAlign: TextAlign.center,
                        ),
                ),
              ),
            ),
            SafeArea(
                child: Padding(
              padding: EdgeInsets.all(20.w),
              child: Row(
                children: [
                  Expanded(
                    child: TextButton(
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Color(0xff009868)),
                      ),
                      onPressed: () {
                        _selectPhotos();
                      },
                      child: Text(
                        '选取图片',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                  SizedBox(
                    width: 20.w,
                  ),
                  Expanded(
                    child: TextButton(
                      style: ButtonStyle(
                        backgroundColor:
                            MaterialStateProperty.all(Color(0xff009868)),
                      ),
                      onPressed: () {
                        _addWatermark();
                      },
                      child: Text(
                        '添加水印',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ],
              ),
            ))
          ],
        ),
      ),
    );
  }
}
